package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : Sonic
 * @version : 1.0
 * @createTime : 2024-12-04  22:56
 * @description : 购物车
 */
@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "ShoppingCartController Api")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * @param: [shoppingCartDTO]
     * @return: com.sky.result.Result
     * @description: 添加购物车
     **/
    @PostMapping("/add")
    @ApiOperation("Add ShoppingCart")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("###### Add ShoppingCart:{}", shoppingCartDTO);
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    /**
     * @param: []
     * @return: com.sky.result.Result<com.sky.entity.ShoppingCart>
     * @description: 查询用户购物车数据，无需传递参数，直接用 Threadlocal获取
     **/
    @GetMapping("/list")
    @ApiOperation("list ShoppingCart")
    public Result<List<ShoppingCart>> list(){
        log.info("###### list ShoppingCart");
        List<ShoppingCart> list = shoppingCartService.showShoppingCart();
        return Result.success(list);
    }

    /**
     * @param: [shoppingCartDTO]
     * @return: com.sky.result.Result
     * @description: 删除购物车中的单个商品
     **/
    @PostMapping("/sub")
    @ApiOperation("delete one ShoppingCart")
    public Result delete(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("###### delete one ShoppingCart");
        shoppingCartService.delete(shoppingCartDTO);
        return Result.success();
    }

    /**
     * @param: []
     * @return: com.sky.result.Result
     * @description: 清空购物车数据
     **/
    @DeleteMapping("/clean")
    @ApiOperation("clean all ShoppingCart")
    public Result clean(){
        log.info("###### clean all ShoppingCart");
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }

}
